package mygang.dao;

import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;

/**
 *
 * @author Volodymyr Lavrynovych <volodya.lavrynovych@gmail.com>
 * date: 15/08/2012
 */
@Component
public abstract class AbstractDAO extends DAO {
    
    private SessionFactory sessionFactory;
    private Session session;
    private HibernateTemplate hibernateTemplate;
    
    public AbstractDAO(){}

    @Autowired
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
        hibernateTemplate = new HibernateTemplate(sessionFactory);
    }
    
    /**
     * Returns hibernate session object
     * @return hibernate session object
     */
    public Session getSession(){
        if(session==null || !session.isOpen()){
            session = sessionFactory.getCurrentSession();
        }
        return session;
    }
    
    /**
     * Returns hibernate template object
     * @return hibernate template object
     */
    public HibernateTemplate getHibernateTemplate(){
        return hibernateTemplate;
    }
}